package MOLE;

import java.util.*;

//��ʱ�ϴ���ͼƬ���ļ�̫�࣬�Ϳ������������ϴ�ͼƬ���ļ������ʱ��������񱾵��ϴ�ͼƬ������Ӧ�ó���ͻᱨ���Could not find a part of the path \\ip\filepath 
//����������������˽�asp.net�İ�ȫ��ϵ�������ģ��ķ���ʹ�ö�д�ļ����С�
//
//�ؼ��ʣ����ģ��
//
//ȱʡ����£�ASP.NETӦ�ó����Ա�����ASPNET�ʺ����У����ʺ�������ͨ�û��飬Ȩ���ܵ�һ�������ƣ��Ա���ASP.NETӦ�ó������еİ�ȫ��������ʱ��Ҫĳ��ASP.NETӦ�ó�����߳����е�ĳ�δ���ִ����Ҫ�ض�Ȩ�޵Ĳ���������ĳ���ļ��Ĵ�ȡ����ʱ����Ҫ���ó������Ӧ��ĳ�δ��븳��ĳ���ʺŵ�Ȩ����ִ�иò��������ַ�����֮Ϊ���ģ�⣨Impersonation����
//
//��Ҫ����һ��ר�е����û�����ִ�иò������ڴ�������ͨ�������ַ�ʽ��
//
//1��   ����web.config
//
//<identity impersonate="true" userName="domain\accountname" password="password" />
//
//2��������ģ��ָ�����û��ʺŸ�����ֻ������Ҫ������Դ�Ĵ���Ρ�ʵ������ο���

 //ģ���ʻ�
public class NetAccess
{
	//ģ���ʻ���Ϣ
	private String ImpersonateDomainName;
	private String ImpersonateAccountName;
	private String ImpersonateAccountPassword;

	public NetAccess()
	{
	}
	public NetAccess(String AccountName, String AccountPassword)
	{
		ImpersonateDomainName = "";
		ImpersonateAccountName = AccountName;
		ImpersonateAccountPassword = AccountPassword;
	}
	public NetAccess(String DomainName, String AccountName, String AccountPassword)
	{
		//���캯���õ�ģ���ʻ���Ϣ����web.config�����õõ�
		ImpersonateDomainName = DomainName;
		ImpersonateAccountName = AccountName;
		ImpersonateAccountPassword = AccountPassword;
	}

	public final boolean UserLog(String AccountName, String AccountPassword)
	{
		return impersonateValidUser("", AccountName, AccountPassword);

	}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region ģ�������֤
	public static final int LOGON32_LOGON_INTERACTIVE = 2;
	public static final int LOGON32_PROVIDER_DEFAULT = 0;

	private WindowsImpersonationContext impersonationContext;

	public static native int LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, tangible.RefObject<IntPtr> phToken);
	static
	{
		System.loadLibrary("advapi32.dll");
	}
	public static native int DuplicateToken(IntPtr hToken, int impersonationLevel, tangible.RefObject<IntPtr> hNewToken);
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion


//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region ģ�������֤
	public final boolean impersonateValidUser(String domain, String userName, String password)
	{
		WindowsIdentity tempWindowsIdentity;
		IntPtr token = IntPtr.Zero;
		IntPtr tokenDuplicate = IntPtr.Zero;

		tangible.RefObject<IntPtr> tempRef_token = new tangible.RefObject<IntPtr>(token);
		boolean tempVar = LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tempRef_token) != 0;
			token = tempRef_token.argValue;
		if (tempVar)
		{
			tangible.RefObject<IntPtr> tempRef_tokenDuplicate = new tangible.RefObject<IntPtr>(tokenDuplicate);
			boolean tempVar2 = DuplicateToken(token, 2, tempRef_tokenDuplicate) != 0;
				tokenDuplicate = tempRef_tokenDuplicate.argValue;
			if (tempVar2)
			{
				tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
				impersonationContext = tempWindowsIdentity.Impersonate();
				if (impersonationContext != null)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}


	public final void undoImpersonation()
	{
		impersonationContext.Undo();
	}
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion


//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region ͼƬ




	/** 
	 �õ�ָ����Դ��������ͼƬ�б�
	 
	 @param iNo
	 @return 
	*/
	public final DataTable GetPictureLib(int iNo)
	{
		//���ر� PicLib��iNo,PicName
		try
		{
			if (impersonateValidUser(ImpersonateAccountName, ImpersonateDomainName, ImpersonateAccountPassword))
			{
				//Insert your code that runs under the security context of a specific user here.
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
					///#region ���ͼƬ��
				String SubPath = "temp";
				String PicPathV = "../picturelib/" + SubPath + "/" + iNo + "/";
				String PicPath = HttpContext.Current.Server.MapPath(PicPathV);

				DataTable tb = new DataTable("PicLib");
				tb.Columns.Add("iNo",Integer.class);
				tb.Columns.Add("PicName",String.class);


				//�Ƿ���ڴ�Ŀ¼
				if (!(new java.io.File(PicPath)).isDirectory())
				{
					return null;
				}

				java.io.File dir = new java.io.File(PicPath);
				FileSystemInfo[] entryies = dir.GetFileSystemInfos();
				FileSystemInfo file;

				for (int i = 0;i < entryies.length;i++)
				{
					file = entryies[i];
					//�����һ���ļ�
					if (file.getClass() == java.io.File.class && (file.Extension.toLowerCase().equals(".jpg") || file.Extension.toLowerCase().equals(".gif") || file.Extension.toLowerCase().equals(".jpeg")))
					{
						DataRow dr = tb.NewRow();
						dr.setItem("iNo", iNo);
						dr.setItem("PicName", file.Name);
						tb.Rows.Add(dr);
					}

				}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
					///#endregion
				undoImpersonation();

				return tb;
			}
			else
			{
				//Your impersonation failed. Therefore, include a fail-safe mechanism here.
				throw new RuntimeException("û�в�����Դ��Ȩ��,��ȷ�Ϲ���Ա���û�Ȩ�ޣ�IIS��Web.Config������ȷ��");
			}

		}

		catch (RuntimeException ex)
		{
			throw (ex);
		}

	}




	/** 
	 �õ��ϴ��ļ�·������ļ���
	 
	 @param fileName
	 @return 
	*/
	private String getFileName(String fileName)
	{
		int position;
		position = fileName.lastIndexOf("\\");
		return fileName.substring(position + 1);
	}



	/** 
	 �õ��ļ�������չ��
	 
	 @param fileName
	 @return 
	*/
	private String getExtendedName(String fileName)
	{
		int position;
		position = fileName.lastIndexOf(".");
		return fileName.substring(position);
	}
	/** 
	 �õ����������
	 
	 @return 
	*/
	private String GetNumberRandom()
	{
		int intNum;
		long lngNum;
		String strNum = String.format("%0S", java.time.LocalDateTime.now());
		strNum = strNum.replace(":","");
		strNum = strNum.replace("-","");
		strNum = strNum.replace(" ","");
		strNum = strNum.replace("/","");
		strNum = strNum.replace("PM","1");
		strNum = strNum.replace("AM","0");

		lngNum = Long.parseLong(strNum);
		Random ran = new Random();
//C# TO JAVA CONVERTER TODO TASK: There is no two-argument version of 'nextInt' in Java:
		intNum = ran.nextInt(1,99999);
		ran = null;
		lngNum += intNum;
		return String.valueOf(lngNum);
	}
	/** 
	 �ϴ�ͼƬʱ�����ݱ��浽�������ļ�����
	 
	 @param iNo
	 @param PicName
	*/
	public final void UploadPictureToLib(int iNo, String PicName, System.Web.UI.HtmlControls.HtmlInputFile myFile)
	{
		try
		{
			if (impersonateValidUser(ImpersonateAccountName, ImpersonateDomainName, ImpersonateAccountPassword))
			{
				//Insert your code that runs under the security context of a specific user here.
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
					///#region �ϴ�ͼƬ�����
				String SubPath = "temp";
				String PicPathV = "../picturelib/" + SubPath + "/" + iNo + "/";
				String PicPath = HttpContext.Current.Server.MapPath(PicPathV);

				//�Ƿ���ڴ�Ŀ¼
				if (!(new java.io.File(PicPath)).isDirectory())
				{
					(new java.io.File(PicPath)).mkdirs();
				}

				//�����ϴ���ͼƬ
				String tmpFileName = getFileName(myFile.PostedFile.FileName);

				if (tmpFileName.indexOf(".") > 0)
				{
					String tmpExtendedName = getExtendedName(tmpFileName);
					if (tmpExtendedName.toLowerCase().equals(".jpg") || tmpExtendedName.toLowerCase().equals(".gif") || tmpExtendedName.toLowerCase().equals(".jpeg"))
					{
						tmpFileName = GetNumberRandom() + tmpExtendedName.toLowerCase();
						myFile.PostedFile.SaveAs(PicPath + tmpFileName);
					}
					else
					{
						throw new RuntimeException("11104");
					}
				}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
					///#endregion
				undoImpersonation();
			}
			else
			{
				//Your impersonation failed. Therefore, include a fail-safe mechanism here.
				throw new RuntimeException("û�в�����Դ��Ȩ��,��ȷ�Ϲ���Ա���û�Ȩ�ޣ�IIS��Web.Config������ȷ��");
			}
		}

		catch (RuntimeException ex)
		{
			throw (ex);
		}
	}
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion

}
//ע��iis������������·�����ؼ���Ҫ�ѱ���Ĭ���Ǳ��ص�����·�����Ӻ�Ŀ¼��ȫ�����ķ�ʽ��
//
//1.����·��ʱ��·�����ֹ����������ַ��
//
//2.����򵼺�����(properties),��·��(home directory)��ѡ����Դ����(the content for this recources should come from),��һ̨�����Ĺ���·�� (a share located in another computer),������д����·��������Ϊ(connected as),������׼�������û��ʺš�
//
//3.·����ȫ��(directory secutity),��֤�ͷ��ʿ���(authentication and access control),�ʻ���Ϣ��Ϊ������û��ʻ�
//
//�����������ӻ�ȡ������Ϣ��

